package com.example.myproject.datasource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {
    // 主数据源
    @Bean(name = "primaryDataSource")  //装配该方法返回值为primaryDataSource管理bean
    @Primary //配置该数据源为主数据源
    @Qualifier("primaryDataSource") // spring装配bean唯一标识
    @ConfigurationProperties(prefix = "spring.datasource.primary") // application.yml文件内配置数据源的前缀
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    // 次数据源
    @Bean(name = "secondaryDataSource")  //装配该方法返回值为secondaryDataSource管理bean
    @Qualifier("secondaryDataSource") // spring装配bean唯一标识
    @ConfigurationProperties(prefix = "spring.datasource.secondary") // application.yml文件内配置数据源的前缀
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}
